package com.tanhua.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.PublishScore;
import com.tanhua.dubbo.api.mongo.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-quanzi", consumerGroup = "tanhua-quanzi-group")
public class PublishScoreListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void onMessage(String message) {
        log.info("====开始计算朋友圈得分=====>"+message);
        //1. 把消息转换为Map对象
        Map<String,Object> map = JSON.parseObject(message, Map.class);
        Long pid = Long.valueOf(map.get("pid").toString());
        Integer type = Integer.valueOf(map.get("type").toString());
        String publishId =map.get("publishId").toString();
        Long uesrId = Long.valueOf(map.get("userId").toString());

        //2. 把消息内容构建一个publishScore对象，
        PublishScore ps = new PublishScore();
        ps.setUserId(uesrId);
        ps.setPublishId(pid); //注意：这里的publishid是需要使用pid
        ps.setDate(System.currentTimeMillis());

        Publish publish = publishApi.findById(publishId);
        //3. 计算本次消息的得分

        switch (type) {
            case 1: //发布动态
                    //文字的计算分数
                     double score = 0;
                    String textContent = publish.getTextContent();
                    if (textContent.length() < 50) {
                        score += 1;
                    } else if (textContent.length() < 100) {
                        score += 2;
                    } else if (textContent.length() >= 100) {
                        score += 3;
                    }
                    //计算图片得分
                    List<String> medias = publish.getMedias();
                    if (medias != null) {
                        score += medias.size();
                    }
                   ps.setScore(score);
                break;
            case 2: {
                ps.setScore(1d);
                break;
            }
            case 3: {
                ps.setScore(5d);
                break;
            }
            case 4: {
                ps.setScore(8d);
                break;
            }
            case 5: {
                ps.setScore(10d);
                break;
            }
            case 6: {
                ps.setScore(-5d);
                break;
            }
            case 7: {
                ps.setScore(-8d);
                break;
            }
            default: {
                ps.setScore(0d);
                break;
            }
        }

        //4. 保存数据
        mongoTemplate.save(ps);
    }
}
